package com.bluetooth.phone;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;

import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.databinding.BindingAdapter;
import androidx.databinding.ObservableBoolean;
import androidx.databinding.ObservableInt;

import com.hsae.bluetooth.phones.databinding.LayoutFanLevelBinding;


public class FanLevel extends ConstraintLayout {


    private static final String TAG = "FanLevel";
    private boolean mIsAutoFan = false;

    private final ObservableInt mMaxFanLevel = new ObservableInt(8);
    private final ObservableInt mCurrentLevel = new ObservableInt(0);
    private LayoutFanLevelBinding mBinding = null;
    private final ObservableBoolean mEnable = new ObservableBoolean(true);
    private final float mDisableAlpha = 0.6f;
    private ManualFanLevel.FanLevelChangeListener mListener = (fanLevel, level) -> {

        Log.e("4444", mEnable.get() + "");
        setCurrentLevel(level);
        callBackLevel(level);
    };

    public FanLevel(Context context) {
        this(context, null);
    }

    public FanLevel(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public FanLevel(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    private void init(Context context) {
        mBinding = LayoutFanLevelBinding.inflate(LayoutInflater.from(context), this, true);
        mBinding.setView(this);
        mBinding.add.setOnClickListener(v -> {

          //  Log.e("",mCurrentLevel.get()+"....##");
            if (mCurrentLevel.get() >= mMaxFanLevel.get()) {
                return;
            }
            int currentLevel = mCurrentLevel.get();

            int level = mCurrentLevel.get() + 1;
            if (level < mMaxFanLevel.get()) {
                //   mBinding.add.playAnimation();
            }
            if (currentLevel != 0) {
                setCurrentLevel(level);
            }
            callBackLevel(level);
        });

        mBinding.reduce.setOnClickListener(v -> {
          //  Log.e("111",mCurrentLevel.get()+"....##");
            if (mCurrentLevel.get() <= 0) {
                return;
            }
            int level = mCurrentLevel.get() - 1;
          //  Log.e("111",level+"....##");
            if (level > 0) {
                //  mBinding.reduce.playAnimation();
            }
            callBackLevel(level);
        });
        updateAddReduceEnable();
        setAutoFan(mIsAutoFan, true);

    }


    public ManualFanLevel.FanLevelChangeListener getListener() {
        return mListener;
    }

    public ObservableInt getMaxFanLevel() {
        return mMaxFanLevel;
    }

    public ObservableInt getCurrentLevel() {
        return mCurrentLevel;
    }

    public ObservableBoolean getEnable() {
        return mEnable;
    }

    public interface OnFanLevelInputChanged {
        void onChanged(int level);
    }

    @BindingAdapter(value = {"bindIsAutoFan", "bindMaxFanLevel", "bindCurrentLevel",
            "bindOnFanLevelInputChanged"}, requireAll = false)
    public static void bind(FanLevel fanLevel, boolean isAutoFan,
                            int maxFanLevel, int currentLevel, OnFanLevelInputChanged listener) {
        fanLevel.setAutoFan(isAutoFan);
        fanLevel.setMaxFanLevel(maxFanLevel);

        fanLevel.setCurrentLevel(currentLevel);
        fanLevel.listener = listener;
    }

    @BindingAdapter(value = {"bindFanLevelEnable"})
    public static void bindEnable(FanLevel view, boolean enable) {
        view.setEnable(enable);
    }

    private void setEnable(boolean enable) {
        mEnable.set(enable);
        updateAddReduceEnable();
        if (enable) {
            Log.e("2223", mEnable.get() + "");
            setCurrentLevel(mCurrentLevel.get());
        }
    }

    private void updateAddReduceEnable() {
        boolean reduceEnable = mCurrentLevel.get() != 0 && mEnable.get();
        mBinding.reduce.setAlpha(reduceEnable ? 1f : 0.6f);

        boolean addEnable = mCurrentLevel.get() != mMaxFanLevel.get() && mEnable.get();
        mBinding.add.setAlpha(addEnable ? 1f : 0.6f);

    }

    private void setAutoFan(boolean isAutoFan) {
        setAutoFan(isAutoFan, false);
    }

    public void setAutoFan(boolean autoFan, boolean forceUpdate) {
        if (!forceUpdate && mIsAutoFan == autoFan) {
            return;
        }
        mIsAutoFan = autoFan;
    }


    private void setMaxFanLevel(int maxFanLevel) {
        mMaxFanLevel.set(maxFanLevel);
        updateAddReduceEnable();
    }

    private OnFanLevelInputChanged listener = null;

    private void callBackLevel(int level) {
        if (listener != null) {
            listener.onChanged(level);
        }

    }

    private void setCurrentLevel(int newLevel) {


        mCurrentLevel.set(newLevel);
        if (!mEnable.get()) {

            return;
        }
        updateAddReduceEnable();
    }

}
